<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::Callbacks</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::Callbacks</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activesupport/lib/active_support/callbacks_rb.html">
                vendor/rails/activesupport/lib/active_support/callbacks.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
<a href="Callbacks.html">Callbacks</a> are hooks into the lifecycle of an
object that allow you to trigger logic before or after an alteration of the
object state.
</p>
<p>
Mixing in this module allows you to define callbacks in your class.
</p>
<p>
Example:
</p>
<pre>
  class Storage
    include ActiveSupport::Callbacks

    define_callbacks :before_save, :after_save
  end

  class ConfigStorage &lt; Storage
    before_save :saving_message
    def saving_message
      puts &quot;saving...&quot;
    end

    after_save do |object|
      puts &quot;saved&quot;
    end

    def save
      run_callbacks(:before_save)
      puts &quot;- save&quot;
      run_callbacks(:after_save)
    end
  end

  config = ConfigStorage.new
  config.save
</pre>
<p>
Output:
</p>
<pre>
  saving...
  - save
  saved
</pre>
<p>
<a href="Callbacks.html">Callbacks</a> from parent classes are inherited.
</p>
<p>
Example:
</p>
<pre>
  class Storage
    include ActiveSupport::Callbacks

    define_callbacks :before_save, :after_save

    before_save :prepare
    def prepare
      puts &quot;preparing save&quot;
    end
  end

  class ConfigStorage &lt; Storage
    before_save :saving_message
    def saving_message
      puts &quot;saving...&quot;
    end

    after_save do |object|
      puts &quot;saved&quot;
    end

    def save
      run_callbacks(:before_save)
      puts &quot;- save&quot;
      run_callbacks(:after_save)
    end
  end

  config = ConfigStorage.new
  config.save
</pre>
<p>
Output:
</p>
<pre>
  preparing save
  saving...
  - save
  saved
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000360">included</a>&nbsp;&nbsp;
      <a href="#M000361">run_callbacks</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="Callbacks/ClassMethods.html" class="link">ActiveSupport::Callbacks::ClassMethods</a><br />
Class <a href="Callbacks/Callback.html" class="link">ActiveSupport::Callbacks::Callback</a><br />
Class <a href="Callbacks/CallbackChain.html" class="link">ActiveSupport::Callbacks::CallbackChain</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000360" class="method-detail">
        <a name="M000360"></a>

        <div class="method-heading">
          <a href="#M000360" class="method-signature">
          <span class="method-name">included</span><span class="method-args">(base)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000360-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000360-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/callbacks.rb, line 205</span>
205:     <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
206:       <span class="ruby-identifier">base</span>.<span class="ruby-identifier">extend</span> <span class="ruby-constant">ClassMethods</span>
207:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000361" class="method-detail">
        <a name="M000361"></a>

        <div class="method-heading">
          <a href="#M000361" class="method-signature">
          <span class="method-name">run_callbacks</span><span class="method-args">(kind, options = {}, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Runs all the callbacks defined for the given options.
</p>
<p>
If a block is given it will be called after each callback receiving as
arguments:
</p>
<pre>
 * the result from the callback
 * the object which has the callback
</pre>
<p>
If the result from the block evaluates to false, the callback chain is
stopped.
</p>
<p>
Example:
</p>
<pre>
  class Storage
    include ActiveSupport::Callbacks

    define_callbacks :before_save, :after_save
  end

  class ConfigStorage &lt; Storage
    before_save :pass
    before_save :pass
    before_save :stop
    before_save :pass

    def pass
      puts &quot;pass&quot;
    end

    def stop
      puts &quot;stop&quot;
      return false
    end

    def save
      result = run_callbacks(:before_save) { |result, object| result == false }
      puts &quot;- save&quot; if result
    end
  end

  config = ConfigStorage.new
  config.save
</pre>
<p>
Output:
</p>
<pre>
  pass
  pass
  stop
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000361-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000361-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/callbacks.rb, line 277</span>
277:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">run_callbacks</span>(<span class="ruby-identifier">kind</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
278:       <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{kind}_callback_chain&quot;</span>).<span class="ruby-identifier">run</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">options</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
279:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>